package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.EmployeeService;
import com.the4man.cnbdw.service.MessageMobileService;
import com.the4man.cnbdw.utils.PageUtil;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.Employee;
import com.the4man.framework.model.Message;
import com.the4man.framework.util.PageJson;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Action("mobileMessageAction")
public class MobileMessageAction extends BaseAction implements ModelDriven<Employee> {
    private Employee emp = new Employee();
    @Autowired
    private MessageMobileService msgMobieleservice;
    @Autowired
    private EmployeeService employeeService;

    public MobileMessageAction() {
    }

    public void getMsgCntBymac() {
        HttpServletRequest request = this.getRequest();
        String mac = request.getParameter("mac");
        String et = request.getParameter("et");
        String type = request.getParameter("type");
        PageJson resultJson = new PageJson();
        boolean var6 = false;

        try {
            Employee emp = this.getEmployeeByMac(mac);
            if(emp == null) {
                resultJson.setReturnMsg("mac地址未绑定用户,mac>>>>[" + mac + "]");
                this.writeJson(resultJson);
                return;
            }

            int cnt = this.msgMobieleservice.getMsgCnt(emp.getId(), type);
            resultJson.setReturnFlag(true);
            resultJson.setReturnMsg("调用成功!");
            resultJson.setObj(Integer.valueOf(cnt));
        } catch (Exception var8) {
            var8.printStackTrace();
            resultJson.setReturnMsg(var8.getMessage());
        }

        this.writeJson(resultJson);
    }

    public void getMsgListPage() {
        HttpServletRequest request = this.getRequest();
        String mac = request.getParameter("mac");
        String et = request.getParameter("et");
        String type = request.getParameter("type");
        String strCurrentPage = request.getParameter("currentPage");
        String strPageSize = request.getParameter("pageSize");
        PageJson resultJson = new PageJson();
//        int currentPage = true;
        boolean var9 = true;

        int currentPage;
        int pageSize;
        try {
            currentPage = Integer.parseInt(strCurrentPage);
            pageSize = Integer.parseInt(strPageSize);
        } catch (Exception var16) {
            resultJson.setReturnMsg("currentPage 或者 pageSize 转换为整型出错");
            this.writeJson(resultJson);
            return;
        }

        try {
            Employee emp = this.getEmployeeByMac(mac);
            if(emp == null) {
                resultJson.setReturnMsg("mac地址未绑定用户,mac>>>>[" + mac + "]");
                this.writeJson(resultJson);
                return;
            }

            Map<String, Object> objMap = new HashMap();
            int cnt = this.msgMobieleservice.getMsgCnt(emp.getId(), type);
            List<Message> list = new ArrayList();
            if(cnt > 0) {
                PageUtil page = PageUtil.build(currentPage, pageSize, cnt);
                list = this.msgMobieleservice.getMsgPage(emp.getId(), type, page.getStartRows(), pageSize);
            }

            objMap.put("totalCnt", Integer.valueOf(cnt));
            objMap.put("list", list);
            resultJson.setReturnFlag(true);
            resultJson.setReturnMsg("调用成功!");
            resultJson.setObj(objMap);
        } catch (Exception var15) {
            var15.printStackTrace();
            resultJson.setReturnMsg(var15.getMessage());
        }

        this.writeJson(resultJson);
    }

    public void updateStateToRead() {
        HttpServletRequest request = this.getRequest();
        String mac = request.getParameter("mac");
        String et = request.getParameter("et");
        String msgIds = request.getParameter("msgIds");
        PageJson resultJson = new PageJson();

        try {
            Employee emp = this.getEmployeeByMac(mac);
            if(emp == null) {
                resultJson.setReturnMsg("mac地址未绑定用户,mac>>>>[" + mac + "]");
                this.writeJson(resultJson);
                return;
            }

            List<String> msgIdListParam = new ArrayList();
            if(msgIds.contains(",")) {
                String[] msgArray = msgIds.split(",");
                String[] var12 = msgArray;
                int var11 = msgArray.length;

                for(int var10 = 0; var10 < var11; ++var10) {
                    String string = var12[var10];
                    msgIdListParam.add(string);
                }
            } else {
                msgIdListParam.add(msgIds);
            }

            boolean bool = this.msgMobieleservice.updateToIsRead(emp.getId(), msgIdListParam);
            if(bool) {
                resultJson.setReturnFlag(true);
                resultJson.setReturnMsg("调用成功!");
            } else {
                resultJson.setReturnMsg("调用失败，传入的参数未匹配到记录!");
            }
        } catch (Exception var13) {
            var13.printStackTrace();
            resultJson.setReturnMsg(var13.getMessage());
        }

        this.writeJson(resultJson);
    }

    public void replyMessage() {
        HttpServletRequest request = this.getRequest();
        String mac = request.getParameter("mac");
        String et = request.getParameter("et");
        String msgId = request.getParameter("msgId");
        String content = "";
        String text = request.getParameter("content");

        try {
            content = URLDecoder.decode(new String(text.getBytes("ISO-8859-1"), "UTF-8"));
            System.out.println("GBK---" + new String(request.getParameter("content").getBytes("ISO-8859-1"), "GBK"));
            System.out.println("UTF-8---" + new String(request.getParameter("content").getBytes("ISO-8859-1"), "UTF-8"));
            System.out.println("UTF-82---" + new String(request.getParameter("content").getBytes("GBK"), "UTF-8"));
        } catch (UnsupportedEncodingException var11) {
            var11.printStackTrace();
        }

        PageJson resultJson = new PageJson();

        try {
            Employee emp = this.getEmployeeByMac(mac);
            if(emp == null) {
                resultJson.setReturnMsg("mac地址未绑定用户,mac>>>>[" + mac + "]");
                this.writeJson(resultJson);
                return;
            }

            boolean bool = this.msgMobieleservice.replyMessage(content, emp.getId(), msgId);
            if(bool) {
                resultJson.setReturnFlag(true);
                resultJson.setReturnMsg("调用成功!");
            } else {
                resultJson.setReturnMsg("调用失败，主信息不存在!");
            }
        } catch (Exception var10) {
            var10.printStackTrace();
            resultJson.setReturnMsg(var10.getMessage());
        }

        this.writeJson(resultJson);
    }

    private Employee getEmployeeByMac(String mac) {
        Employee result = null;

        try {
            List<Employee> list = this.employeeService.selectEmployeeByMac(mac);
            if(list != null && list.size() > 0) {
                result = (Employee)list.get(0);
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return result;
    }

    public Employee getModel() {
        return this.emp;
    }
}
